#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cmath> 
#include <vector>
#include "Function.hpp"
#include "Interpolation.hpp"
#define PI 3.14159265358979323846

class Fx : public Function {
public:
    double operator() (double x) const override {
        return 16 * pow(sin(x), 3);
    }
};

class Fy : public Function {
public:
    double operator() (double x) const override {
        return 13 * cos(x) - 5 * cos(2 * x) - 2 * cos(3 * x) - cos(4 * x);
    }
};

std::vector<double> polynomial(double t, int n) {
    std::vector<std::vector<double>> controlPoints;
    Fx fx;
    Fy fy;

    for (int k = 0; k < n; k++) {
        double angle = 2 * PI * k / n;
        controlPoints.push_back({fx(angle), fy(angle)});
    }
    BezierInterpolation bezier(controlPoints);
    return bezier.interpolate(t);
}

int main() {
    for (int n = 10; n <= 160; n *= 4) {
        std::ofstream file("data_m" + std::to_string(n) + ".csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        for (double t = 0; t <= 1; t += 0.01) {
            std::vector<double> y = polynomial(t, n);
            file << y[0] << "," << y[1] << std::endl;
        }

        file.close();
    }
    return 0;
}
